package controllers;

import models.Person;
import models.PersonDocument;
import models.Profile;
import play.data.binding.Binder;
import play.db.Model;
import play.exceptions.TemplateNotFoundException;
import play.mvc.With;

import java.lang.reflect.Constructor;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: bicho
 * Date: 29/01/12
 * Time: 4:10
 * To change this template use File | Settings | File Templates.
 */

@With(Secure.class)
@Check(Profile.SUPERADMIN)
@CRUD.For(PersonDocument.class)
public class PeopleDocuments extends CRUD {

    public static void listForPerson(String personId) {
        Person person = StageUtils.getPersonFromPersonId(personId);
        List<PersonDocument> objects = person.personDocuments;
        ObjectType type = ObjectType.get(getControllerClass());
        int count = objects.size();
        int totalCount = objects.size();
        int page = 1;
        String orderBy = null;
        String order = null;
        render(type, person, objects, count, totalCount, page, orderBy, order);
    }


    public static void blankForPerson(String personId) throws Exception {
        Person person = StageUtils.getPersonFromPersonId(personId);
        ObjectType type = ObjectType.get(getControllerClass());
        notFoundIfNull(type);
        Constructor<?> constructor = type.entityClass.getDeclaredConstructor();
        constructor.setAccessible(true);
        Model object = (Model) constructor.newInstance();
        try {
            render(type, object, person);
        } catch (TemplateNotFoundException e) {
            render("CRUD/blank.html", type, object);
        }
    }

    public static void create() throws Exception {
        ObjectType type = ObjectType.get(getControllerClass());
        notFoundIfNull(type);
        Constructor<?> constructor = type.entityClass.getDeclaredConstructor();
        constructor.setAccessible(true);
        PersonDocument object = (PersonDocument) constructor.newInstance();
        Binder.bindBean(params.getRootParamNode(), "object", object);
        validation.valid(object);
        if (validation.hasErrors()) {
            renderArgs.put("error", play.i18n.Messages.get("crud.hasErrors"));
            try {
                render(request.controller.replace(".", "/") + "/blank.html", type, object);
            } catch (TemplateNotFoundException e) {
                render("CRUD/blank.html", type, object);
            }
        }
        object._save();
        flash.success(play.i18n.Messages.get("crud.created", type.modelName));
        redirect(request.controller + ".listForPerson", object.person.id);
    }

    public static void save(String id) throws Exception {
        ObjectType type = ObjectType.get(getControllerClass());
        notFoundIfNull(type);
        PersonDocument object = PersonDocument.findById(Long.parseLong(id));
        notFoundIfNull(object);
        Binder.bindBean(params.getRootParamNode(), "object", object);
        validation.valid(object);
        if (validation.hasErrors()) {
            renderArgs.put("error", play.i18n.Messages.get("crud.hasErrors"));
            try {
                render(request.controller.replace(".", "/") + "/show.html", type, object);
            } catch (TemplateNotFoundException e) {
                render("CRUD/show.html", type, object);
            }
        }
        object._save();
        flash.success(play.i18n.Messages.get("crud.saved", type.modelName));
        if (params.get("_save") != null) {
            redirect(request.controller + ".listForPerson", object.person.id);
        }
        redirect(request.controller + ".show", object._key());
    }


}
